#include "sdram_test.h"
#include <string.h>
#include "common_utils.h"

#define SDRAM_EXAMPLE_DATALEN    32*1024

volatile uint32_t SRAM_write_buff_Cache[SDRAM_EXAMPLE_DATALEN];
volatile uint32_t SRAM_read_buff_Cache[SDRAM_EXAMPLE_DATALEN];

volatile uint32_t sdram_cache[SDRAM_EXAMPLE_DATALEN] BSP_PLACE_IN_SECTION(".sdram");
volatile uint32_t sdram_nocache[SDRAM_EXAMPLE_DATALEN]  BSP_PLACE_IN_SECTION(".nocache_sdram");


/*******************************************************************************
 *
 ******************************************************************************/
#define EXAMPLE_SDRAM_START_ADDRESS (0x68000000U)
#define EXAMPLE_DTCM_START_ADDRESS  (0x20000000U)
#define EXAMPLE_SRAM_START_ADDRESS  (0x220B0000U)

uint32_t sdram_write_count = 0;
/* SRAM<--->SDRAM 读写测试 */
void SDRAMReadWrite32Bit_test(void)
{
       uint32_t index;
       uint32_t datalen = SDRAM_EXAMPLE_DATALEN ;
       uint32_t i = 0;

       APP_PRINT("##############################################################\r\n");
       APP_PRINT("##############################################################\r\n");
       APP_PRINT("********** Write to SDRAM! **********\r\n");


       memset((uint32_t *)SRAM_write_buff_Cache, 0 ,datalen);
       memset((uint32_t *)SRAM_read_buff_Cache, 0 ,datalen);
       memset((uint32_t *)sdram_cache, 0 ,datalen);
       memset((uint32_t *)sdram_nocache, 0 ,datalen);
       for( i=0; i<datalen; i++)
       {
           SRAM_write_buff_Cache[i] = i;
       }


       for (index = 0; index < datalen; index++)
       {
           sdram_cache[index]  = SRAM_write_buff_Cache[index];  //source 为 SRAM
       }

       APP_PRINT("**********Write SDRAM End! **********\r\n");
       APP_PRINT("**********Write %d bytes to SDRAM  **********\r\n", SDRAM_EXAMPLE_DATALEN*4);

       R_BSP_SoftwareDelay(10, BSP_DELAY_UNITS_MILLISECONDS);

       APP_PRINT("##############################################################\r\n");
       APP_PRINT("##############################################################\r\n");
       APP_PRINT("**********Read from SDRAM! **********\r\n");


        for (index = 0; index < datalen; index++)
        {
            SRAM_read_buff_Cache[index] = sdram_cache[index];  //读SDRAM
        }

        index = 0;
        while(index<datalen)
        {
            if(SRAM_read_buff_Cache[index] != SRAM_write_buff_Cache[index])
            {
                APP_PRINT("**********SRDAM test failed **********\r\n");
            }
            index++;
        }

        APP_PRINT("**********Read %d bytes from SDRAM  **********\r\n", SDRAM_EXAMPLE_DATALEN*4);
        APP_PRINT("**********SRDAM read End! **********\r\n");
        APP_PRINT("**********SRDAM test pass **********\r\n");


}
